#!/usr/bin/env bash
# Author: Z.kc (273816036@qq.com)
# 执行后台yii command
# admin/proctected/commands
# 如果要在crontab中执行的话需要ln -s /usr/local/sbin/admin-cmd* /usr/bin/



HYD_PATH="/opt/web/hydt3"
ADMIN_PATH="$HYD_PATH/admin"
YIIC="php -f yiic164t3.php"

CMD_LOG="/tmp/admin-cmd-$(date +%Y-%m-%d).log"

# 将错误输出重定向到文件
exec 2>> $CMD_LOG

function log() {
    echo "$(date +%Y-%m-%d\ %H:%M:%S)|$@" | tee -a $CMD_LOG
}


# 判断参数个数，至少2个
(( $# < 2 )) && echo 'usage: admin-cmd.sh Command Action [arg1][ arg2][ argn]'


# 所有人可写可执行
umask 000

# command
CMD=$1
# action
ACT=$2

LOG="$HYD_PATH/logs/$(date +%Y/%m/%d)/admin-cmd-${CMD}_${ACT}-$(date +%Y%m%d).log"
LOG_DIR=`dirname $LOG`
# 创建日志目录
! [ -f $LOG_DIR ] && log "mkdir -p $LOG_DIR" && mkdir -p $LOG_DIR
cd $ADMIN_PATH/protected/
log "cd $ADMIN_PATH/protected/"

# 要执行的命令
CMD_STR="$YIIC $@ 2>&1 >> $LOG"
#$CMD_STR
log "$CMD_STR"

$YIIC $@ 2>&1 >> $LOG
ret=$?
# 记录执行结果
log "exit status:$? $YIIC $@"
exit $ret